class_name BallStateCarried
extends BallState

const OFFSET_FROM_PLAYER := Vector2(10, 4)
const DRIBBLE_FREQUENCE := 10.0
const DRIBBLE_INTENSITY := 3.0

var dribble_time := 0.0

func _enter_tree() -> void:
	assert(carried != null, "carried doesn't exist!!!")
	GameEvent.ball_possessed(carried.full_name)
	
func _exit_tree() -> void:
	carried = null
	GameEvent.ball_released()

func _process(delta: float) -> void:
	var vx := 0.0
	dribble_time += delta

	if carried.velocity != Vector2.ZERO:
		if carried.velocity.x != 0:
			vx = cos(dribble_time * DRIBBLE_FREQUENCE) * DRIBBLE_INTENSITY
		if carried.heading.x > 0:
			animation_player.play("roll")
		else:
			animation_player.play_backwards("roll")
		animation_player.advance(0)
	else:
		animation_player.play("idle")
	process_gravity(delta)
	ball.position = carried.position + Vector2(vx + carried.heading.x * OFFSET_FROM_PLAYER.x, OFFSET_FROM_PLAYER.y)
